package ru.papergames.model.proxies
{
	import flash.external.ExternalInterface;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	
	public class PersistentProxy extends BaseProxy
	{
		
		public static const NAME:String = 'PersistentProxy';
		public static const LOCAL_OBJ_NAME:String = "papergames_local_so";
		
		private var _browserOriented:Boolean = false;
		
		public function PersistentProxy(browserOriented:Boolean = false)
		{
			_browserOriented = browserOriented;
			
			super(NAME, 
				  SharedObject.getLocal(LOCAL_OBJ_NAME + (_browserOriented ? browserType : '') ) 
				);
		}
		
		private function get browserType():String {
			if (!ExternalInterface.available) return '';
			
			var bType:String = ExternalInterface.call('game_getBrowserType');
			
			return bType;
		}
		
		private function get so():SharedObject {
			return this.data as SharedObject;
		}
		
		public function storeData(dataKey:String, data:Object):Boolean {
			var rez:String = '';
			
			if (dataKey) {
			
				so.data[dataKey] = data;
				try {
					rez = so.flush();
				} catch (e:Error) {
					return false;
				}
			
			}
			
			return rez == SharedObjectFlushStatus.FLUSHED;
		}
		
		public function retrieveData(dataKey:Object):Object {
			return so.data[dataKey];
		}
		
	}
}